const { model } = require("mongoose");
const { parkingsModel } = require("./models/parkingsModel");
//渲染（出租人）
module.exports.getParkingsById = async function (params) {
    //获取数据总条数
    const total = await parkingsModel.countDocuments();
    //计算总页数
    const pages = Math.ceil(total / params.pageSize);
    //populate 用来关联查询集合
    const parkings = await parkingsModel.find({ lessorsId: params.lessorsId, delete: false }).populate({
        path: "lessorsId",
    }).limit(params.pageSize - 0).skip((params.currentPage - 1) * params.pageSize);
    return {
        total,
        pages,
        parkings
    }
}
//渲染（管理员）
module.exports.getParkings = async function (params) {
    //获取数据总条数
    const total = await parkingsModel.countDocuments();
    //计算总页数
    const pages = Math.ceil(total / params.pageSize);
    //populate 用来关联查询集合
    const parkings = await parkingsModel.find({ delete: false, state: 1 }).populate({
        path: "lessorsId",
    }).limit(params.pageSize - 0).skip((params.currentPage - 1) * params.pageSize);
    return {
        total,
        pages,
        parkings
    }
}

//增加
module.exports.addParkings = async function (parkings) {
    const data = await parkingsModel.create(parkings);
    return data;
}

//删除(管理员)
module.exports.deleteParkings = async function (_id) {
    const data = await parkingsModel.updateOne({ _id }, { delete: true });
    return data;
}

//删除（出租人）
module.exports.deleteParkingsById = async function (_id) {
    const data = await parkingsModel.updateOne({ _id }, { delete: true });
    return data;
}

//修改
module.exports.confirmChange = async function ({ _id, address, number, property, cycle, price }) {
    const data = await parkingsModel.updateOne({ _id }, { address, number, property, cycle, price })
    return data
}

//修改审核状态
module.exports.updataAuditParkings = async function ({ _id, state }) {
    const data = await parkingsModel.updateOne({ _id }, { state })
    return data;
}


//获取所有数据
module.exports.getAll = async function (code) {
    const pageSize = code.pageSize;
    const currentPage = code.currentPage
    const nums = await parkingsModel.countDocuments({state:4});//获取数据的总条数
    const pagesTotal = Math.ceil(nums / pageSize);//向上取整(小数部分也需要所以需要向上取整
    const data = await parkingsModel.find({ state: 4 }).populate('lessorsId').limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);//用于设置要跳过的条数
    return {
        data,
        nums,
        pagesTotal,
    };
}


//查找通过id
module.exports.getListById = async function (id) {
    console.log(id)
    const data = await parkingsModel.find({ _id: id }).populate('lessorsId')
    return data;
}


//购买车票修改状态
module.exports.changeParkingState = async function (id) {
    // console.log(id)
    const data = await parkingsModel.updateOne({ _id: id }, { state: 5 })
    return data;
}

//归还车位
module.exports.backParking = async function (id) {
    console.log(id)
    const data = await parkingsModel.updateOne({ _id: id }, { state: 4 })
    return data;
}

